package com.surfilter.risk.service.impl;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.surfilter.risk.constant.ConstantStr;
import com.surfilter.risk.dto.BusinessDetail;
import com.surfilter.risk.enums.CodeStateEnum;
import com.surfilter.risk.exception.ConditionIDException;
import com.surfilter.risk.exception.QueryException;
import com.surfilter.risk.mapper.BusinessDetailsMapper;
import com.surfilter.risk.mapper.PunishMapper;
import com.surfilter.risk.service.BusinessService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SearchServiceImpl
 * Dis:
 * Created by grom on 2017/5/15.
 */
@Service("businessService")
public class BusinessServiceImpl implements BusinessService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BusinessDetailsMapper businessDetailsMapper;

    /**
     * 企业详情接口数据
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Map<String, Object> businessDetails(Integer id) {
        Map<String, Object> result = Maps.newHashMap();
        Map<String, Object> data = Maps.newHashMap();
        result.put(ConstantStr.CODE, -1);
        result.put(ConstantStr.MSG, StringUtils.EMPTY);
        result.put(ConstantStr.DATA, data);
        if (id == null) {
            logger.info("[QX_business]: company_id is null ");
            return result;
        }
        Map baseInfo = businessDetailsMapper.findCompanyBaseInfo(id);
        if (null == baseInfo || baseInfo.size() <= 0) return result;
        Map risk_Ratio = businessDetailsMapper.findRiskRatio(id);
        Map ranking = getCompanyRanking(id);
        List<Map> survey = getCompanyPunishSurvey(id);
        data.putAll(baseInfo);                       //基本信息
        data.putAll(risk_Ratio);                     //风险指数
        data.putAll(ranking);                        //排名信息
        data.put(ConstantStr.PUNIISH, survey);     //处罚概况信息
        result.put(ConstantStr.CODE, 0);
        result.put(ConstantStr.DATA, data);
        return result;
    }

    /**
     * 获取公司处罚概况
     *
     * @param param
     * @return
     */
    private List<Map> getCompanyPunishSurvey(Integer param) {
        return businessDetailsMapper.findPunishSurvey(param);
    }

    /**
     * 获取企业处罚惩戒总数、惩戒金额、地域分布三个维度的排名
     *
     * @param param
     * @return
     */
    private Map getCompanyRanking(Integer param) {
        Map ranking = new HashMap();
        Map PunishTotal = businessDetailsMapper.findPunishTotal(param);
        Map AmountPositonRanking = businessDetailsMapper.findAmountPositonRanking(param);
        Map AmountInternalRanking = businessDetailsMapper.findAmountInternalRanking(param);
        Map PunishMoney = businessDetailsMapper.findPunishMoney(param);
        Map MoneyPositonRanking = businessDetailsMapper.findMoneyPositonRanking(param);
        Map MoneyInternalRanking = businessDetailsMapper.findMoneyInternalRanking(param);
        Map PunishAreaTotal = businessDetailsMapper.findPunishAreaTotal(param);
        Map PunishAreaPositionRanking = businessDetailsMapper.findPunishAreaPositionRanking(param);
        Map PunishAreaInternalRanking = businessDetailsMapper.findPunishAreaInternalRanking(param);
        if (null != PunishTotal) ranking.putAll(PunishTotal);
        if (null != AmountPositonRanking) ranking.putAll(AmountPositonRanking);
        else ranking.put("countregion", 1);
        if (null != AmountInternalRanking) ranking.putAll(AmountInternalRanking);
        else ranking.put("countall", "1");
        if (null != PunishMoney) ranking.putAll(PunishMoney);
        if (null != MoneyPositonRanking) ranking.putAll(MoneyPositonRanking);
        else ranking.put("moneyregion", 1);
        if (null != MoneyInternalRanking) ranking.putAll(MoneyInternalRanking);
        else ranking.put("moneyall", 1);
        if (null != PunishAreaTotal) ranking.putAll(PunishAreaTotal);
        if (null != PunishAreaPositionRanking) ranking.putAll(PunishAreaPositionRanking);
        else ranking.put("localregion", 1);
        if (null != PunishAreaInternalRanking) ranking.putAll(PunishAreaInternalRanking);
        else ranking.put("localall", 1);
        return ranking;
    }
}
